<?php
/**
 * This is the template for generating a CRUD controller class file.
 */

use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
    $searchModelAlias = $searchModelClass . 'Search';
}

/* @var $class ActiveRecordInterface */
$class = $generator->modelClass;

$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();

echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;

use Yii;
use <?= ltrim($generator->modelClass, '\\') ?>;
<?php if (!empty($generator->searchModelClass)) { ?>
use <?= ltrim($generator->searchModelClass, '\\') ?>;
<?php } ?>
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
use yii\filters\VerbFilter;
use yii\helpers\Html;
/**
 * <?= $controllerClass ?> implements the CRUD EasyUi actions for <?= $modelClass ?> model.
 */
class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
{

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all <?= $modelClass ?> models.
     * @return mixed
     */
    public function actionIndex($_query=0) {
        if($_query) {
            Yii::$app->getResponse()->format = 'json';
            <?php if (!empty($generator->searchModelClass)) { ?>
                $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
                return $searchModel->loadData(Yii::$app->getRequest());
            <?php } ?>
        }

        Yii::$app->view->title = '<?= $table_comment ?>管理';
        return $this->render('index');
    }

    /**
     * Creates a new <?= $modelClass ?> model.
     * @return mixed
     */
    public function actionCreate()
    {
        if(Yii::$app->request->isGet) {
            return $this->render('create', ['action' => 'create']);
        }

        Yii::$app->getResponse()->format = 'json';
        $post = Yii::$app->request->post();
        $model = new <?= $modelClass ?>();
        //set your variable default value here when new
        //$model->nama_kelas = Html::encode($post['nama_kelas']);

        if ($post) {
            $data['<?= $modelClass ?>'] = $post;
            if ($model->load($data) && $model->save(true)) {
                return [
                    'type' => 'success',
                    'message' => '增加成功'
                ];
            } else {
				$errors = $model->getErrors();

				return [
					'type'    => 'failed',
					'message' => '增加失败',
					'errors'  => $errors,
				];
            }
        }
        return[
            'type' => 'error',
            'message' => '非法数据',
            'data' => $model->toArray(),
        ];
    }

	private function loadData($id) {
		$data = <?=$modelClass?>::find()->where(['id'=>$id])->asArray()->one();
		if(empty($data)) {
			throw new \Exception('数据不存在');
		}
		return $data;
	}

    /**
     * Update <?= $modelClass ?> model.
     * @return mixed
    */
    public function actionSave($id)
    {
		if(empty($id)) {
			$id = Yii::$app->request->post('id');
		}
        if(Yii::$app->request->isGet) {
			$data = $this->loadData($id);
            return $this->render('save', ['data' => $data, 'action' => 'save']);
        }

        Yii::$app->getResponse()->format = 'json';
        $post = Yii::$app->request->post();
        if ($id != null) {
            $model = <?= $modelClass ?>::findOne($id);
            if ($model === null) {
                return [
                    'type' => 'error',
                    'message' => '数据不存在'
                ];
            }
            //set your variable default value here when update
            //$model->nama_kelas = Html::encode($post['nama_kelas']);

	        if ($post) {
	            $data['<?= $modelClass ?>'] = $post;
	            if ($model->load($data) && $model->save(true)) {
	                return [
	                    'type' => 'success',
	                    'message' => '修改成功'
	                ];
	            } else {
					$errors = $model->getErrors();

					return [
						'type'    => 'failed',
						'message' => '修改失败',
						'errors'  => $errors,
					];
	            }
	        }
		}

        return [
            'type' => 'error',
            'message' => '非法数据',
            'data' => $model->toArray(),
        ];
    }

    /**
    * Update <?= $modelClass ?> model.
    * @return mixed
    */
    public function actionView($id)
    {
		$data = $this->loadData($id);
		return $this->render('view', ['data' => $data, 'action' => 'view']);
    }

    /**
     * delete action
     * @return mixed
     */
    public function actionDelete($id) {
        Yii::$app->getResponse()->format = 'json';
        $model = <?= $modelClass ?>::findOne($id);
        if ($model === null) {
            return [
                'type' => 'error',
                'message' => '数据不存在'
            ];
        }
        //$model->aktif = 0;
        if ($model->delete()) {
        //if ($model->save(false)) {
            return [
                'type' => 'success',
                'message' => '删除成功'
            ];
        } else {
            return [
                'type' => 'failed',
                'message' => '删除失败'
            ];
        }
    }
}
